/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *      base_service.cpp
 * 位 置:
 *      src/biz/base_service.cpp
 * 描 述:
 *		基础服务类
 * 备 注:
 *      无
 * 修 改:
 *      2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "pub/db.h"
#include "pub/str.h"
#include "pub/log.h"
#include "pub/err.h"
#include "biz/pub_acct.h"
#include "biz/pub_log.h"
#include "biz/pub_priv.h"
#include "biz/pub_serial.h"
#include "biz/pub_business.h"
#include "biz/pub_sys.h"
#include "biz/base_service.h"

using namespace std;

//----------------------------------------------
//	构造函数
//----------------------------------------------
BaseService::BaseService()
{
	ConnectionPoolWrapper* pool =  ConnectionPoolWrapper::getInstance();
	conn = pool->createConnection();

	sys		= new PubSystem(conn);
	acct	= new PubAccount(conn);
	priv	= new PubPriv(conn);
	serial	= new PubSerial(conn);
	log		= new PubLog(conn);
}

//----------------------------------------------
//	析构函数
//----------------------------------------------
BaseService::~BaseService()
{
	delete sys;
	delete acct;
	delete priv;
	delete serial;
	delete log;

	pool->terminateConnection(conn);
	ConnectionPoolWrapper::freeInstance();
}

//----------------------------------------------
//	检查输入参数
//----------------------------------------------
void BaseService::checkData()
{
    //检查功能代码
    if (!serviceCode.match("^[0-9]{6}$"))
    {
        Log::throwError("功能代码不合法");
    }

    //检查受理方式
    if (!acceptMethod.match("^[01234]$"))
    {
        Log::throwError("受理方式不合法");
    }

    //检查渠道代码
    if (!channelCode.match("^[0-9]{1,3}$"))
    {
        Log::throwError("渠道代码不合法");
    }

    //检查地区代码
    if (!regionCode.match("^[0-9]{1,4}$"))
    {
        Log::throwError("地区代码不合法");
    }

    //检查网点代码
    if (!branchCode.match("^[0-9]{1,9}$"))
    {
        Log::throwError("网点代码不合法");
    }

    //检查操作员编号
    if (!opID.match("^[0-9]{1,8}$"))
    {
        Log::throwError("操作员编号不合法");
    }
}

//----------------------------------------------
//  初始化
//----------------------------------------------
void BaseService::init()
{
    //初始化系统参数
	sys->init();

	tradeDate	= sys->tradeDate;
	tradeTime	= sys->tradeTime;
	sysDate		= sys->sysDate;
	sysTime		= sys->sysTime;

    //获取业务代码
	businessCode = PubBusiness::getBusinessCode(serviceCode);
}

//----------------------------------------------
//  检查功能权限
//----------------------------------------------
void BaseService::checkPriv()
{
	//检查系统状态是否允许执行该业务
	priv->sysStatus		= sys->sysStatus;
	priv->acceptMethod	= acceptMethod;
	priv->serviceCode	= serviceCode;
	priv->checkSys();
}

//----------------------------------------------
//  检查条件是否满足
//----------------------------------------------
void BaseService::checkCondition()
{
}

//----------------------------------------------
//  写操作日志
//----------------------------------------------
void BaseService::writeLog()
{
	//写入操作日志数据
	log->serviceCode	= serviceCode;
	log->channelCode	= channelCode;
	log->regionCode		= regionCode;
	log->branchCode		= branchCode;
	log->opID			= opID;
	log->opDate			= tradeDate;
	log->opTime			= tradeTime;
	log->ipAddr			= "";
	log->macAddr		= "";
	log->returnCode		= returnCode;
	log->returnMsg		= returnMsg;
	log->writeLog();
}

//----------------------------------------------
//  执行
//----------------------------------------------
void BaseService::execute()
{
	try
	{
		//检查输入参数
    	checkData();

		//初始化
		init();

		//检查功能权限
		checkPriv();

		//检查条件是否满足
		checkCondition();

		//功能处理
		process();

		//提交事务
		conn->commit();
	}
	catch(exception& e)
	{
		//回滚事务
		conn->rollback();
	}

	//写入操作日志
	writeLog();
}
